package com.nineclock.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.document.dto.DocUserCollaborationDTO;
import com.nineclock.document.mapper.DocCollaborationsMapper;
import com.nineclock.document.mapper.DocFileMapper;
import com.nineclock.document.pojo.DocCollaborations;
import com.nineclock.document.pojo.DocFile;
import com.nineclock.document.service.DocCollaborationsService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangyan
 * @date 2021/4/7 19:34
 */
@Service
@Transactional
public class DocCollaborationsServiceImpl implements DocCollaborationsService {

    @Autowired
    private SysUserFeign sysUserFeign;
    @Autowired
    private DocFileMapper fileMapper;
    @Autowired
    private DocCollaborationsMapper collaborationsMapper;

    /**
     * 根据文件ID获取用户信息 , 标识那些是拥有者 , 那些是协作者
     *
     * @param fileId
     * @return
     */
    @Override
    public List<DocUserCollaborationDTO> pagingUsers(Long fileId) {
        //1. 参数校验
        if (fileId == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //2. 查询当前企业的所有用户 --> (拥有者 协作者)
        List<SysCompanyUserDTO> allCompanyUser = sysUserFeign.queryAllCompanyUser().getData();

        //3. 根据文档ID 获取文档信息
        DocFile docFile = fileMapper.selectById(fileId);
        //从文档信息中获取拥有者ID
        Long createUserId = docFile.getCreateUserId();

        //4. 根据文件ID 查询文件的协作者信息
        LambdaQueryWrapper<DocCollaborations> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocCollaborations::getFileId, fileId);
        List<DocCollaborations> collaborationsList = collaborationsMapper.selectList(wrapper);
        //遍历 获取协作者ID
        List<Long> collIdList = collaborationsList.stream().map(docCollaborations -> {
            return docCollaborations.getCollaborationId(); //协作者ID
        }).collect(Collectors.toList()); //封装到集合

        //5. 组装数据并返回 --> 标识那些是协作者, 那些是拥有者
        //遍历所有用户
        List<DocUserCollaborationDTO> collaborationDTOList =
                allCompanyUser.stream().map(sysCompanyUserDTO -> {

                    DocUserCollaborationDTO collaborationDTO = new DocUserCollaborationDTO();

                    Long companyUserId = sysCompanyUserDTO.getId();
                    collaborationDTO.setId(companyUserId); //员工ID
                    collaborationDTO.setImgUrl(sysCompanyUserDTO.getImageUrl()); //员工图像
                    collaborationDTO.setPhone(sysCompanyUserDTO.getMobile()); //员工手机号
                    collaborationDTO.setUsername(sysCompanyUserDTO.getUserName()); //员工姓名

                    //三元表达式
                    /*
                    当前员工是拥有者 赋值1,否则, 如果协作者集合包含当前员工,赋值2,否则,什么也不是 0
                     */
                    int state = createUserId == companyUserId ? 1 : collIdList.contains(companyUserId) ? 2 : 0;

                    collaborationDTO.setState(state); //员工状态标识

                    return collaborationDTO;
                }).collect(Collectors.toList());

        return collaborationDTOList;
    }

    //todo 添加/删除协作者
}
